var loginBtn = document.querySelector('header .member > div')
loginBtn.onclick = function () {
    var loginDialog = document.querySelector('section.login')
    loginDialog.style.visibility = 'visible'
}

var loginCloseBtn = document.querySelector('section.login i.icon-close')
loginCloseBtn.onclick = function () {
    var loginDialog = document.querySelector('section.login')
    loginDialog.style.visibility = 'hidden'
}

var loginBlueBox = document.querySelector('section.login .menu')
loginBlueBox.onmousedown = function (evt) {
    var oldE = evt || window.event
    document.onmousemove = function (evt) {
        var e = evt || window.event
        var left = e.clientX - oldE.offsetX
        var top = e.clientY - oldE.offsetY
        if (left < 0) left = 0
        if (top < 0) top = 0
        var loginDialog = document.querySelector('section.login .item')
        var maxLeft = (window.innerWidth || document.documentElement.clientWidth) - loginDialog.offsetWidth
        var maxTop = (window.innerHeight || document.documentElement.clientHeight) - loginDialog.offsetHeight
        if (left > maxLeft) left = maxLeft
        if (top > maxTop) top = maxTop
        loginDialog.style.position = 'absolute'
        loginDialog.style.left = left + 'px'
        loginDialog.style.top = top + 'px'
    }
    document.onmouseup = function () {
        document.onmousemove = null
    }
}


var num = 60
var loginCaptchaBtn = document.querySelector('section.login .captcha')
loginCaptchaBtn.onclick = function () {

    var captchaObj = document.querySelector('section.captcha')
    captchaObj.style.visibility = 'visible'
    document.querySelector('section.captcha i.icon-close').onclick = function () {
        captchaObj.style.visibility = 'hidden'
    }
    var dragBtn = captchaObj.querySelector('.drag .btn')
    dragBtn.onmousedown = function () {
        document.onmousemove = function (evt) {
            var e = evt || window.event
            var left = e.clientX - (captchaObj.offsetLeft - captchaObj.offsetWidth / 2) - dragBtn.offsetWidth

            dragBtn.style.left = left + 'px'
            captchaObj.querySelector('.check img:nth-of-type(2)').style.left = left + 'px'
        }
        document.onmouseup = function () {
            document.onmousemove = null
            var value = parseInt(dragBtn.style.left)
            if (value >= 260 && value <= 265) {
                captchaObj.style.visibility = 'hidden'
                loginCaptchaBtn.innerText = num + '秒后重发'
                loginCaptchaBtn.disabled = true
                t = setInterval(function () {                    // 判断：a:清除定时器,b:页面重置,c:全局num终止,d:终止代码执行
                    if (num <= 1) {
                        clearInterval(t)
                        loginCaptchaBtn.innerText = '重新发送'
                        num = 5
                        loginCaptchaBtn.disabled = false
                        return
                    }
                    num--
                    loginCaptchaBtn.innerText = num + '秒后重发'
                }, 1000)

            }
            dragBtn.style.left = '0px'
            captchaObj.querySelector('.check img:nth-of-type(2)').style.left = '0px'
        }
    }
    return

}

var loginSubmitBtn = document.querySelector('section.login .submit')

loginSubmitBtn.onclick = function () {
    var isOk = 0
    var mobileObj = document.querySelector('.mobile')
    if (mobileObj.value == '' || mobileObj.value.length != 11 || !Number(mobileObj.value)) {
        document.querySelector('section.login .form > p:nth-of-type(3)').innerText = '手机格式有误📱'
    } else {
        isOk++
        document.querySelector('section.login .form > p:nth-of-type(3)').innerText = ''
    }
    var passwordObj = document.querySelector('.password')
    if (passwordObj.value == '' || passwordObj.value.length != 4 || !Number(passwordObj.value)) {
        document.querySelector('section.login .form > p:nth-of-type(4)').innerText = '验证码只能是4位村数字🔢'
    } else {
        isOk++
        document.querySelector('section.login .form > p:nth-of-type(4)').innerText = ''
    }
    if (isOk == 2) {
        alert('登录成功')
    }
}